package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HolidayHomeStatisticsMapper;
import com.ruoyi.system.domain.HolidayHomeStatistics;
import com.ruoyi.system.service.IHolidayHomeStatisticsService;

/**
 * 节假日返乡统计Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
public class HolidayHomeStatisticsServiceImpl implements IHolidayHomeStatisticsService
{
    @Autowired
    private HolidayHomeStatisticsMapper holidayHomeStatisticsMapper;

    /**
     * 查询节假日返乡统计
     *
     * @param staId 节假日返乡统计主键
     * @return 节假日返乡统计
     */
    @Override
    @DataScope(deptAlias = "hhs",userAlias = "hhs")
    public HolidayHomeStatistics selectHolidayHomeStatisticsByStaId(Long staId)
    {
        return holidayHomeStatisticsMapper.selectHolidayHomeStatisticsByStaId(staId);
    }

    /**
     * 查询节假日返乡统计列表
     *
     * @param holidayHomeStatistics 节假日返乡统计
     * @return 节假日返乡统计
     */
    @Override
    @DataScope(deptAlias = "hhs",userAlias = "hhs")
    public List<HolidayHomeStatistics> selectHolidayHomeStatisticsList(HolidayHomeStatistics holidayHomeStatistics)
    {
        return holidayHomeStatisticsMapper.selectHolidayHomeStatisticsList(holidayHomeStatistics);
    }

    /**
     * 新增节假日返乡统计
     *
     * @param holidayHomeStatistics 节假日返乡统计
     * @return 结果
     */
    @Override
    public int insertHolidayHomeStatistics(HolidayHomeStatistics holidayHomeStatistics)
    {
        return holidayHomeStatisticsMapper.insertHolidayHomeStatistics(holidayHomeStatistics);
    }

    /**
     * 修改节假日返乡统计
     *
     * @param holidayHomeStatistics 节假日返乡统计
     * @return 结果
     */
    @Override
    public int updateHolidayHomeStatistics(HolidayHomeStatistics holidayHomeStatistics)
    {
        return holidayHomeStatisticsMapper.updateHolidayHomeStatistics(holidayHomeStatistics);
    }

    /**
     * 批量删除节假日返乡统计
     *
     * @param staIds 需要删除的节假日返乡统计主键
     * @return 结果
     */
    @Override
    public int deleteHolidayHomeStatisticsByStaIds(Long[] staIds)
    {
        return holidayHomeStatisticsMapper.deleteHolidayHomeStatisticsByStaIds(staIds);
    }

    /**
     * 删除节假日返乡统计信息
     *
     * @param staId 节假日返乡统计主键
     * @return 结果
     */
    @Override
    public int deleteHolidayHomeStatisticsByStaId(Long staId)
    {
        return holidayHomeStatisticsMapper.deleteHolidayHomeStatisticsByStaId(staId);
    }

    @Override
    public String importPayment(List<HolidayHomeStatistics> contractPaymentList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(contractPaymentList) || contractPaymentList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<HolidayHomeStatistics> existList = selectHolidayHomeStatisticsList(null);
        for (HolidayHomeStatistics importData : contractPaymentList)
        {
            try {

                boolean userFlag = false;
                for (HolidayHomeStatistics entry : existList) {
                    if (entry.getJobNumber().equals(importData.getJobNumber())&&entry.getDateDeparture().equals(importData.getDateDeparture())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertHolidayHomeStatistics(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + importData.getJobNumber() +"在"+importData.getDateDeparture()+ " 请假数据"+"导入成功");
                } else if (updateSupport) {
                    updateHolidayHomeStatistics(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + importData.getJobNumber() +"在"+importData.getDateDeparture()+ " 请假数据"+ " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + importData.getJobNumber() +"在"+importData.getDateDeparture()+ " 请假数据"+ " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + importData.getJobNumber() +"在"+importData.getDateDeparture()+ " 请假数据"+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
